#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "ST7305_UI.h"
#include "st7305_1p54_driver.h"
#include "U8g2_for_ST7305.h"
#include "sdkconfig.h"
#include "image.h"

#define DC_PIN   CONFIG_YDP_TFT_DC_GPIO
#define RES_PIN  CONFIG_YDP_TFT_RES_GPIO
#define CS_PIN   CONFIG_YDP_TFT_CS_GPIO
#define SCLK_PIN CONFIG_YDP_TFT_CLK_GPIO
#define SDIN_PIN CONFIG_YDP_TFT_MOSI_GPIO

ST7305_1p54_driver display((gpio_num_t)DC_PIN, (gpio_num_t)RES_PIN, (gpio_num_t)CS_PIN, (gpio_num_t)SCLK_PIN, (gpio_num_t)SDIN_PIN);
U8G2_FOR_ST7305 u8g2_for_st7305;


extern "C" void task_display(void *param){
    static uint8_t di = 0;
    uint32_t ms_current;
    static uint8_t rx = 30;
    static bool right = true;
    static uint32_t last_increase_time = 0;
    static uint32_t frame_time = 0;
    static uint32_t ms_base = xTaskGetTickCount() * portTICK_PERIOD_MS;
    static uint32_t frame_base = display.frame_num;
    while(1){       
        if(display.frame_update_done_flag){
            ms_current = xTaskGetTickCount() * portTICK_PERIOD_MS;

            display.clearDisplay();
            if(ms_current - last_increase_time >= 5000){    //每5秒切换一次方向
                di++;
                if (di==4) di=0;
                last_increase_time = ms_current;
            }             
            if(right) {
                if(ms_current - frame_time >= 1000/50){     //期望运动速度每秒50像素.屏最高帧率是51Hz，运动太快可能卡顿。
                    rx++;
                    if(rx==170) right = false;
                    frame_time = ms_current;
                }
            } else {
                if(ms_current - frame_time >= 1000/50){
                    rx--;
                    if(rx==30) right = true;
                    frame_time = ms_current;
                }
            }

            display.setRotation(di);
            display.drawLine(0, 80, 200, 80, ST7305_COLOR_BLACK);
            display.drawCircle(rx, 120, 25, ST7305_COLOR_BLACK);
            display.drawRectangle(rx-25, 148, rx+25, 198, ST7305_COLOR_BLACK);
            display.drawTriangle(rx-20, 152, rx+20, 152, rx, 192, ST7305_COLOR_BLACK);
            u8g2_for_st7305.setCursor(0,16);
            u8g2_for_st7305.println("It's build by ESP-IDF."); 
            u8g2_for_st7305.println("The chip is ESP32-C6.");
            u8g2_for_st7305.print(ms_current);
            u8g2_for_st7305.println(" ms");
            u8g2_for_st7305.print("data rate:");
            u8g2_for_st7305.print((display.frame_num-frame_base)*1000/(ms_current - ms_base));
            u8g2_for_st7305.println(" fps");
            display.display();
        }
        //vTaskDelay(pdMS_TO_TICKS(10));
        //如果延时小于10ms，会触发任务看门狗，但是不影响运行。能得到最高数据帧率。
    }
}



extern "C" void app_main(void)
{     
    display.initialize();
    display.Low_Power_Mode();
    display.display_on(true);
    display.display_Inversion(false);
    u8g2_for_st7305.begin(display);
    display.clearDisplay();
    display.display();
    u8g2_for_st7305.setFontDirection(0);
    u8g2_for_st7305.setForegroundColor(ST7305_COLOR_BLACK); 
    u8g2_for_st7305.setBackgroundColor(ST7305_COLOR_WHITE);
    u8g2_for_st7305.setFont(u8g2_font_helvR14_tf);
    u8g2_for_st7305.setFontMode(1);
    for(int i=0;i<4;i++){
        display.clearDisplay();
        display.setRotation(i);
        display.drawXBM(50,50,image_width,image_height,image_bits);
        display.display();
        vTaskDelay(pdMS_TO_TICKS(1000));
    }

    xTaskCreate(task_display, "task_display", 4096, NULL, 3, NULL);
}
